package com.snow.starter.global;

import com.snow.starter.SnowAutoConfiguration;
import com.snow.starter.interceptor.AuthInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 全局web配置
 *
 * @author by 13119
 */

@Slf4j
public class SnowWebConfiguration implements WebMvcConfigurer {

    final AuthInterceptor authInterceptor;
    final SnowAutoConfiguration snowAutoConfiguration;

    public SnowWebConfiguration(AuthInterceptor authInterceptor, SnowAutoConfiguration snowAutoConfiguration) {
        this.authInterceptor = authInterceptor;
        this.snowAutoConfiguration = snowAutoConfiguration;
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        if (authInterceptor != null) {
            log.info("权限拦截器注入：\n拦截地址：{}\n免拦截地址：{}", snowAutoConfiguration.getHandlerPaths(), snowAutoConfiguration.getHandlerExceptPaths());
            registry.addInterceptor(authInterceptor).addPathPatterns(snowAutoConfiguration.getHandlerPaths()).excludePathPatterns(snowAutoConfiguration.getHandlerExceptPaths());
        } else {
            log.warn("权限拦截器未注入，可能存在安全隐患");
        }
    }
}
